package com.ruoyi.standingbook.recycle.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.standingbook.recycle.domain.bo.RecycleBo;
import com.ruoyi.standingbook.recycle.domain.vo.RecyclePicVo;
import com.ruoyi.standingbook.recycle.domain.vo.RecycleVo;
import com.ruoyi.standingbook.recycle.service.IRecycleService;
import com.ruoyi.standingbook.recycle.service.impl.RecycleExcelService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 垃圾清运
 *
 * @author cc
 * @date 2023-04-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/standingbook/recycle")
public class RecycleController extends BaseController {

    private final IRecycleService iRecycleService;

    private final RecycleExcelService recycleExcelService;

    /**
     * 查询垃圾清运列表
     */
    @SaCheckPermission("standingbook:recycle:list")
    @GetMapping("/list")
    public TableDataInfo<RecycleVo> list(RecycleBo bo, PageQuery pageQuery) {
        return iRecycleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出垃圾清运列表
     */
    @SaCheckPermission("standingbook:recycle:export")
    @Log(title = "垃圾清运", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RecycleBo bo, HttpServletResponse response) {
        List<RecycleVo> list = iRecycleService.queryList(bo);
        ExcelUtil.exportExcel(list, "垃圾清运", RecycleVo.class, response);
    }

    /**
     * 导出垃圾清运日报
     */
    @SaCheckPermission("standingbook:recycle:export")
    @Log(title = "垃圾清运", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDailyReport")
    public void exportDailyReport(RecycleBo bo,HttpServletResponse response) {
        recycleExcelService.exportDailyReport(bo,response);
    }

    /**
     * 获取垃圾清运详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("standingbook:recycle:query")
    @GetMapping("/{id}")
    public R<RecycleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iRecycleService.queryById(id));
    }

    /**
     * 新增垃圾清运
     */
    @SaCheckPermission("standingbook:recycle:add")
    @Log(title = "垃圾清运", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RecycleBo bo) {
        return toAjax(iRecycleService.insertByBo(bo));
    }

    /**
     * 修改垃圾清运
     */
    @SaCheckPermission("standingbook:recycle:edit")
    @Log(title = "垃圾清运", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RecycleBo bo) {
        return toAjax(iRecycleService.updateByBo(bo));
    }

    /**
     * 删除垃圾清运
     *
     * @param ids 主键串
     */
    @SaCheckPermission("standingbook:recycle:remove")
    @Log(title = "垃圾清运", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iRecycleService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 按指定日期查询智慧大屏垃圾回收台账
     * @param date 指定日期
     * @return
     */
    @SaIgnore()
    @GetMapping("/recycleByDate")
    public R<List<RecycleVo>> recycleByDate(String date) {
        return R.ok(iRecycleService.queryListByDate(date));
    }

    @SaIgnore()
    @GetMapping("/checkedPic")
    public R<List<RecyclePicVo>> checkedPic(String date)  {
        return R.ok(iRecycleService.getCheckedPicByDate(date));
    }

    @SaIgnore()
    @GetMapping("/cleaningPic")
    public R<List<RecyclePicVo>> cleaningPic(String date)  {
        return R.ok(iRecycleService.getCleaningPicByDate(date));
    }

    @SaIgnore()
    @GetMapping("/comingPic")
    public R<List<RecyclePicVo>> comingPic(String date)  {
        return R.ok(iRecycleService.getComingPicByDate(date));
    }
}
